<?php
session_start(); 
$login="fournisseur1";

$tabPrix['patate']="50";
$tabPrix['tomate']="60";
$tabPrix['radis']="20";

function AfficherForm1() {
   echo '<h2>Choisir votre produit</h2><form method="post" action="' . htmlentities($_SERVER['PHP_SELF']).'
   	">
  	<table>
        <tr>
          <td>Produit:</td> 
          <td><input type="text" name="label" size="30" /></td> 
        </tr>
                  
        <tr>
          <td>Quantité:</td> 
          <td><input type="text" name="qte" size="3" /></td> 
        </tr>
        
        <tr>
          <td colspan="2"><input type="submit" name="subf1" value="Submit" /></td> 
        </tr>
      </table>
    </form>';
 
}

function AfficherForm2($label, $quantite) {
   echo '<h2>Valider votre produit</h2><form method="post" action="' . htmlentities($_SERVER['PHP_SELF']).'
   	">
  	<table>
        <tr>
          <td>Produit:</td> 
          <td><input type="text" name="label" size="30" value="'.htmlentities($label).'"/></td> 
        </tr>
                  
        <tr>
          <td>Quantité:</td> 
          <td><input type="text" name="qte" size="3"  value="'.htmlentities($quantite).'"/></td> 
        </tr>
        
        <tr>
          <td colspan="2"><input type="submit" name="subf2" value="Submit" /></td> 
        </tr>
      </table>
    </form>';
 
}

function debut() {echo "<html><head></head><body>";  }
  
function fin() {echo "</body></html>";   }

if (isset($_POST['label']) and !array_key_exists($_POST['label'],$tabPrix)) {
	debut();
		echo '<font color="red">des '.htmlentities($_POST['label']).'s ne sont pas dans nos produits.</font>';
	afficherForm1();
	fin();
	exit(0);
 
}
if (!isset($_POST['subf1']) and !isset($_POST['subf2'])) {
	debut();
	afficherForm1();
	fin();
	exit(0);
  }
  if (isset($_POST['subf1'])) {
    debut();
	
	// POST submission, validate input
    if (trim($_POST['label']) == '') {
      die('ERREUR: Pas de label');
    }
    if (trim($_POST['qte']) == '') {
      die('ERREUR: Pas de quantité');
    }
    afficherForm2(htmlentities($_POST['label']), htmlentities($_POST['qte']));
    $_SESSION["label"]=htmlentities($_POST['label']);
    $_SESSION["prixUnitaire"]=$tabPrix[$_SESSION["label"]];
    $_SESSION["label"]=htmlentities($_POST['label']);
    $_SESSION["login"]=$login;
    fin();
    exit(0);
  }
  if (isset($_POST['subf2'])) {
  	if ($_POST['label']!=$_SESSION['label']) {
  		debut();
  		echo '<font color="red">des '.htmlentities($_POST['label']).'s ne sont pas des '.$_SESSION['label'].'s</font>';
  		$_SESSION["label"]=$_POST['label'];
    	$_SESSION["prixUnitaire"]=$tabPrix[$_SESSION["label"]];
    
  		afficherform2(htmlentities($_POST['label']), htmlentities($_POST['qte']));
  		fin();
  		exit(0);
  	}
	
	debut();
  	echo "<H2>JE VALIDE:</H2><pre>";
  	echo "\n * Login:". $_SESSION['login'];
  	echo "\n * Produit:". htmlentities($_POST['label']);
  	echo "\n * Quantité:". htmlentities($_POST['qte']);  	  	
  	echo "\n * prixUnitaire:". $_SESSION['prixUnitaire'];
  	echo "\n * prix Total:". $_SESSION['prixUnitaire']*htmlentities($_POST['qte']);
  	echo "</pre>";
  	fin();
  }
?>
